package com.flca.frw.components
{
	import flash.display.DisplayObject;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ArrayUtil;
	
	import com.flca.frw.config.FwMasterData;
	import com.flca.frw.components.BaseCtrlr;
	import com.flca.frw.services.interfaces.SecurityService;
	import com.flca.frw.services.interfaces.ServiceFactory;
	import com.flca.frw.util.GuiUtil;
	import com.flca.frw.util.Hash;
	import com.flca.frw.util.ValidationUtil;
	
	public class ChangePasswordCtrlr extends BaseCtrlr
	{
		public static const CLASS_NAME:String = "com.flca.frw.components.ChangePasswordCtrlr";
		
		public function ChangePasswordCtrlr()
		{
			readOnly = false;
			hasModel = false;
			closeAfterSave = true;
		}
		
		override public function getDisplayName():String
		{
			return "Password - Maintenance";
		}
		
		override public function getClassName():String
		{
			return CLASS_NAME;
		}
		
		override public function getView():DisplayObject
		{
			if (view == null)
			{
				view = new ChangePasswordView();
				castView().ctrlr = this;
			}
			
			return view;
		}
		
		override protected function validateView():String
		{
			var result:String;
			
			result = super.validateView();
			if (result == null)
			{
				if (castView().tiPassword.text != castView().tiPasswordCopy.text)
				{
					result = "The two entered passwords are not identical.";
				}
			}
			
			return result;
		}
		
		override protected function persistModel(aResultCallback:Function, aFaultCallBack:Function):void
		{
			var svc:SecurityService;
			
			svc = ServiceFactory.getInstance().getSecurityService();
			svc.invokeChangePassword(castView().tiPassword.text, aResultCallback, aFaultCallBack);
		}
		
		override protected function getValidators():Array
		{
			var result:Array;
			
			result = new Array();
			result.push(castView().vldPassword);
			result.push(castView().vldPasswordCopy);
			
			return result;
		}
		
		override protected function manageGuiState():void
		{
			ValidationUtil.enableValidators(getValidators(), true);
			
			return;
		}
		
		internal function btnSaveClicked():void
		{
			performSave();
		}
		
		private function castView():ChangePasswordView
		{
			return view as ChangePasswordView;
		}
	}
}